package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.CollectDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.CollectMapper;
import com.iflytek.model.Collect;
import com.iflytek.service.CollectService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.CollectVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private CollectService collectService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<List<CollectVO>> findAll() {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        List<Collect> collectList = collectMapper.selectList(queryWrapper);
        List<CollectVO> collectVOList = CopyUtil.copyList(collectList, CollectVO.class);
        return Result.success(collectVOList);
    }

    /**
     * saveCollect
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveCollect(CollectDTO dto) {
        //Collect collect = new Collect();
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Collect collect = CopyUtil.copy(dto, Collect.class);
        if (StringUtils.isEmpty(collect.getId())) {
            if (StringUtils.isEmpty(collect)) {
                return Result.error("1", "保存失败");
            }
//            collect.setId(UuidUtil.getShortUuid());

        }
        collectMapper.insert(collect);
        return Result.success(true);
    }

    /**
     * updateCollect
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateCollect(CollectDTO dto) {
        Collect collect = new Collect();
        BeanUtils.copyProperties(dto, collect);
        int count = collectMapper.updateById(collect);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<CollectVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Collect collect = collectMapper.selectById(id);
        CollectVO collectVO = CopyUtil.copy(collect, CollectVO.class);

        return Result.success(collectVO);
    }


    /**
     * @param id
     * @return
     */
    @Override
    public Result<CollectVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = collectMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Result<PageVO<CollectVO>> findPage(PageDTO<CollectDTO> dto) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper();

        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Collect> collects = collectMapper.selectList(queryWrapper);

        List<CollectVO> collectVOList = new ArrayList<>();
        collectVOList = CopyUtil.copyList(collects, CollectVO.class);

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        PageVO<CollectVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(collectPageInfo.getTotal());
        pageVO.setRecords(collectVOList);
        return Result.success(pageVO);
    }

}
